package kz.simplex.photobox.action;

import kz.simplex.photobox.model.*;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("photoHome")
public class PhotoHome extends EntityHome<Photo> {

	@In(create = true)
	AlbumHome albumHome;
	@In(create = true)
	UserHome userHome;
	@In(create = true)
	StatusHome statusHome;

	public void setPhotoId(Integer id) {
		setId(id);
	}

	public Integer getPhotoId() {
		return (Integer) getId();
	}

	@Override
	protected Photo createInstance() {
		Photo photo = new Photo();
		return photo;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Album album = albumHome.getDefinedInstance();
		if (album != null) {
			getInstance().setAlbum(album);
		}
		User owner = userHome.getDefinedInstance();
		if (owner != null) {
			getInstance().setOwner(owner);
		}
		Status status = statusHome.getDefinedInstance();
		if (status != null) {
			getInstance().setStatus(status);
		}
	}

	public boolean isWired() {
		return true;
	}

	public Photo getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
